#define DS1307_SDA PIN_B2
#define DS1307_SCL PIN_B3
#define DS1307_ID 0xd0
#use i2c(master, sda=DS1307_SDA, scl=DS1307_SCL)

/*=======================================================================================================================
                                                   Initialize DS1307
======================================================================================================================*/
void RTC_Init()
{
   output_float(DS1307_SCL);
   output_float(DS1307_SDA);
}
/*======================================================================================================================
                                               Write data one byte to DS1307
 =====================================================================================================================*/
void RTC_Write(byte address, BYTE data)
{
   short int status;
   i2c_start();
   i2c_write(0xd0);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   i2c_start();
   status=i2c_write(0xd0);
   while(status==1)
   {
      i2c_start();
      status=i2c_write(0xd0);
   }
}
/*=====================================================================================================================
                                                 Read data one byte from DS1307
======================================================================================================================*/
BYTE RTC_Read(byte address)
{
   BYTE data;
   i2c_start();
   i2c_write(DS1307_ID);
   i2c_write(address);
   i2c_start();
   i2c_write(0xd1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}

void RTC_RD()
{
   sec=RTC_Read(0);   // read second
   min=RTC_Read(1);   // read minute
   hour=RTC_Read(2);  // read hour
   day=RTC_Read(3);   // read day
   date=RTC_Read(4);  // read date
   month=RTC_Read(5); // read month
   year=RTC_Read(6);  // read year
   delay_ms(10);
}

void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
        I2C_start();

        I2C_write(DS1307_ID);   /* connect to DS1307 */
        I2C_write(0x00);                /* Request RAM address at 00H */

        I2C_write(ss);                  /* Write sec on RAM address 00H */
        I2C_write(mm);                  /* Write min on RAM address 01H */
        I2C_write(hh);                  /* Write hour on RAM address 02H */

        I2C_stop();             /* Stop i2c bus */
}

void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
        I2C_start();

        I2C_write(DS1307_ID);   /* connect to DS1307 */
        I2C_write(0x04);                /* Request RAM address at 04H */

        I2C_write(dd);                  /* Write date on RAM address 04H */
        I2C_write(mm);                  /* Write month on RAM address 05H */
        I2C_write(yy);                  /* Write year on RAM address 06H */

        I2C_stop();                             /* Stop i2c bus */
}
